<?php
class MyController extends CController
{
	public $css;// 网页样式
	public $js;//脚本
	public $pageTitle;// 网页标题
	public $keywords;// 网页关键词
	public $description;// 网页描述
	public $userinfo;// 登陆用户信息
	const PAGE_SIZE = 30;
	public $order = 'DESC';
	public function init()
	{
		parent::init();
		//权限验证
		if(preg_match('#^(\w+/\w+)#', Yii::app()->request->pathInfo,$m))
		{
			$path = strtolower($m[1]);
		}
		else
		{
			$path = Yii::app()->request->pathInfo;
		}
		if(!$this->_userinfo() && ($path != 'site/login' && $path != 'site/seccode'))
		{
			$this->redirect('/site/login');
		}
	}

	/**
	 * 得到当前登陆用户的信息
	 */
	public function _userinfo()
	{
		if(!Yii::app()->user->isGuest && !$this->userinfo)
		{
			$user_id = Yii::app()->user->getState('uid');
			if($user_id)
			{
				$this->userinfo = Admin::model()->find("id = $user_id");
			}
		}
		return $this->userinfo;
	}
	/**
	 * 不加载布局的视图输出
	 */
	public function display($filename, $data = array())
	{
		header('Content-Type: text/html; charset=utf-8');
		header('Pragma: no-cache');
		header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
		$this->renderPartial($filename, $data);
	}

	/**
	 * AJAX输出
	 */
	public static function ajaxDisplay($msg)
	{
		header('Content-Type: text/html; charset=utf-8');
		header('Pragma: no-cache');
		header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');

		die($msg);
	}

	/**
	 * 后台管理中心AJAX输出错误！
	 */
	public static function ajaxError($msg, $error = 1)
	{
		header('Content-Type: text/html; charset=utf-8');
		header('Pragma: no-cache');
		header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');

		$result = array();
		$result['msg'] = $msg;
		$result['error'] = $error;
		die(CJSON::encode($result));
	}

	/**
	 * DWZ框架普通AJAX请求错误消息提示
	 */
	public static function alert_error($msg = '操作失败')
	{
		$data = array();
		$data['message'] = $msg;
		$data['statusCode'] = 300;
		self::ajaxDisplay(CJSON::encode($data));
	}

	/**
	 * DWZ框架普通AJAX请求成功消息提示
	 */
	public static function alert_ok($msg = '操作成功', $data = array())
	{
		if(!$data)
		{
			$data['callbackType'] = 'forward';//表示刷新
		}
		$data['message'] = $msg;
		$data['statusCode'] = 200;
		self::ajaxDisplay(CJSON::encode($data));
	}

	/**
	 * 验证码
	 */
	public function actions() {
		return array(
		'captcha'=> array(
		'class' => 'CCaptchaAction',//加载外部的action class
		'backColor' => 0xffffff,//设置验证码图片背景色属性
		'width' => 100,
		'height' => 40,
		'padding' => 3,
		'maxLength' => 2,
		//'transparent' => TRUE,// 是否需要刷新按钮
		'testLimit' => 10,//三次输入错误时就换一张
		'transparent' => TRUE,//显示为透明*/
		),
		);
	}

	/**
	 * 注销
	 */
	public function actionLogout()
	{
		$refer = Yii::app()->request->getParam('refer');
		$forward = Yii::app()->request->getParam('forward');
		if(!$refer)
		{
			$refer = '/';
		}
		if(!$forward)
		{
			$forward = '/';
		}

		Yii::app()->user->logout();
		if($refer && $forward == '/')
		{
			$this->redirect($refer);
		}
	}

	public static function hash_password($password)
	{
		return md5(md5($password));
	}

	/**
	 * 得到用户的IP
	 */
	public static function getUserHostAddress()
	{
		switch(true)
		{
			case ($ip=getenv("HTTP_X_FORWARDED_FOR")):
				break;
			case ($ip=getenv("HTTP_CLIENT_IP")):
				break;
			default:
				$ip=getenv("REMOTE_ADDR") ? getenv("REMOTE_ADDR") : '127.0.0.1';
		}
		if (strpos($ip, ', ') > 0)
		{
			$ips = explode(', ', $ip);
			$ip = $ips[0];
		}
		return $ip;
	}
}